﻿
Partial Class WebForm_AddControl_AddWorkItemControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            Me.BindWorkItemForInsert()
        End If

    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        '將清潔內容加到 CheckboxList
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim item As New ListItem

            item.Text = TryCast(e.Row.FindControl("lblSubText"), Label).Text
            item.Value = TryCast(e.Row.FindControl("lblSubValue"), Label).Text
            TryCast(e.Row.FindControl("cbList"), CheckBoxList).Items.Add(item)

        End If
        
    End Sub

    ''' <summary>
    ''' 將相同項目的 ItemGroup 作 RowSpan
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub gv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.DataBound


        '將相同項目的 ItemGroup 作 RowSpan
        Dim i As Integer = 1

        For Each wkItem As GridViewRow In Me.gv.Rows
            If wkItem.RowIndex <> 0 Then
                Dim firstSubRow As Integer = wkItem.RowIndex - i
                Dim s1 As String = DirectCast(wkItem.FindControl("lblWorkItemGroup"), Label).Text.Trim()
                Dim s2 As String = DirectCast(Me.gv.Rows(firstSubRow).FindControl("lblWorkItemGroup"), Label).Text.Trim()

                If s1 = s2 Then
                    Me.gv.Rows(firstSubRow).Cells(0).RowSpan += 1
                    wkItem.Cells(0).Visible = False
                    i = i + 1
                Else
                    Me.gv.Rows((wkItem.RowIndex)).Cells(0).RowSpan += 1

                    i = 1

                End If
            Else
                wkItem.Cells(0).RowSpan = 1
            End If
        Next

    End Sub


End Class

Partial Class WebForm_AddControl_AddWorkItemControl

#Region "繫結資料"

    Public Sub BindWorkItemForInsert()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select a.Value,a.Text, ")
        sql.AppendFormat("b.Value as SubValue,b.Text as SubText ")
        sql.AppendFormat("from CodeMapping a inner join CodeMapping b ")
        sql.AppendFormat("on a.TableName+'_'+a.ColumnName+'_'+a.Value = b.TableName ")
        sql.AppendFormat("where a.TableName = 'WorkItem' and a.ColumnName='ItemGroup' ")
        sql.AppendFormat("and a.Active = 'Y' and b.Active = 'Y' ")

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

    End Sub

    Public Sub BindWorkItemForEdit(ByVal WorkHeaderID As String)

        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As New System.Data.DataTable

        sql.AppendFormat("select * from workItem ")
        sql.AppendFormat("where WorkHeaderID = '{0}' ", WorkHeaderID)

        dt = dat.GetData(sql)

        For i As Integer = 0 To gv.Rows.Count - 1

            For j As Integer = 0 To dt.Rows.Count - 1

                If dt.Rows(j)("ItemGroupID").ToString = TryCast(Me.gv.Rows(i).FindControl("lblItemGroupID"), Label).Text _
                    And dt.Rows(j)("ItemID").ToString = TryCast(Me.gv.Rows(i).FindControl("lblSubValue"), Label).Text Then

                    TryCast(Me.gv.Rows(i).FindControl("cbList"), CheckBoxList).Items(0).Selected = True
                    TryCast(Me.gv.Rows(i).FindControl("txtAmount"), TextBox).Text = dt.Rows(j)("Amount").ToString()

                End If

            Next

        Next

    End Sub

#End Region

    Public Function GetWorkItem() As System.Data.DataTable
        '將清潔內容存起來
        Dim dt As New System.Data.DataTable
        Dim dc1 As New System.Data.DataColumn
        Dim dc2 As New System.Data.DataColumn
        Dim dc3 As New System.Data.DataColumn

        dc1.ColumnName = "ItemGroupID"
        dt.Columns.Add(dc1)
        dc2.ColumnName = "ItemID"
        dt.Columns.Add(dc2)
        dc3.ColumnName = "Amount"
        dt.Columns.Add(dc3)

        For i As Integer = 0 To Me.gv.Rows.Count - 1
            If TryCast(Me.gv.Rows(i).FindControl("cbList"), CheckBoxList).Items(0).Selected Then
                Dim dr As System.Data.DataRow = dt.NewRow()
                dr("ItemGroupID") = TryCast(Me.gv.Rows(i).FindControl("lblItemGroupID"), Label).Text
                dr("ItemID") = TryCast(Me.gv.Rows(i).FindControl("lblSubValue"), Label).Text
                dr("Amount") = TryCast(Me.gv.Rows(i).FindControl("txtAmount"), TextBox).Text

                dt.Rows.Add(dr)
            End If
        Next

        Return dt
    End Function

End Class
